/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import pojo.LoaiSanPham;
import dao.LoaiSanPhamDAO;
import java.awt.Component;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import org.jdesktop.swingx.JXTable;

/**
 *
 * @author hieupham
 */
public class QLLoaiSPGui extends javax.swing.JDialog {
 private JErrorProvider errSanPham;
 private List<LoaiSanPham>loaiSPs;
    /**
     * Creates new form QLLoaiSPGui
     */
    public QLLoaiSPGui(java.awt.Frame parent, boolean modal) throws Exception {
        super(parent, modal);
        initComponents();
        SetTabOrder();
        Autoload();
    }
    private void SetTabOrder(){
        IndexedFocusTraversalPolicy indexedFocusTraversalPolicy=new IndexedFocusTraversalPolicy();
        indexedFocusTraversalPolicy.addIndexedComponents(new Component[]{txtMaLoaiSP,txtTenLoaiSP,btnThem,btnXoa,btnSua,btnClear});
        setFocusTraversalPolicy(indexedFocusTraversalPolicy);
    }
    private void Autoload()throws Exception{
     loaiSPs=(List<LoaiSanPham>) LoaiSanPhamDAO.LayDanhSach(LoaiSanPham.class,MainGUI.nguoiDung);
     Utility.CreateColumnsFromDB(LoaiSanPham.class, loaiSPs, tbDanhSachLoaiSP, new String[]{"Mã Loại Sản Phẩm","Tên Loạii Sản Phẩm"}, null);          
      // txtMaLoaiSP.setDragEnabled(false);
    }
     private int KiemTraTruocKhiThem(){
        if(txtTenLoaiSP.getText().trim().isEmpty()==true){
            errSanPham.setError(txtTenLoaiSP, "required");      
            return 1;
        }
        return 0;
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtMaLoaiSP = new javax.swing.JTextField();
        txtTenLoaiSP = new javax.swing.JTextField();
        btnThem = new javax.swing.JButton();
        btnXoa = new javax.swing.JButton();
        btnSua = new javax.swing.JButton();
        btnTimLoaiSP = new javax.swing.JButton();
        btnClear = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbDanhSachLoaiSP = new org.jdesktop.swingx.JXTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Quản Lý Loại Sản Phẩm");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Loại Sản Phẩm"));

        jLabel1.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        jLabel1.setText("Mã Loại SP");

        jLabel2.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        jLabel2.setText("Tên Loại SP");

        txtMaLoaiSP.setEditable(false);
        txtMaLoaiSP.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N

        txtTenLoaiSP.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N

        btnThem.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        btnThem.setText("Thêm");
        btnThem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThemActionPerformed(evt);
            }
        });

        btnXoa.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        btnXoa.setText("Xóa");
        btnXoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnXoaActionPerformed(evt);
            }
        });

        btnSua.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        btnSua.setText("Sửa");
        btnSua.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSuaActionPerformed(evt);
            }
        });

        btnTimLoaiSP.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        btnTimLoaiSP.setText("Tìm");
        btnTimLoaiSP.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTimLoaiSPActionPerformed(evt);
            }
        });

        btnClear.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        btnClear.setText("Clear");
        btnClear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnClearActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(14, 14, 14)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(btnThem)))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(4, 4, 4)
                        .addComponent(txtMaLoaiSP, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 69, Short.MAX_VALUE)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txtTenLoaiSP, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(77, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(2, 2, 2)
                        .addComponent(btnXoa, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnSua, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnClear)
                        .addGap(18, 18, 18)
                        .addComponent(btnTimLoaiSP)
                        .addGap(60, 60, 60))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtMaLoaiSP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(txtTenLoaiSP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnThem)
                    .addComponent(btnXoa)
                    .addComponent(btnSua)
                    .addComponent(btnTimLoaiSP)
                    .addComponent(btnClear))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Danh Sách Loại Sản Phẩm"));

        tbDanhSachLoaiSP.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        tbDanhSachLoaiSP.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        tbDanhSachLoaiSP.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbDanhSachLoaiSPMouseClicked(evt);
            }
            public void mousePressed(java.awt.event.MouseEvent evt) {
                tbDanhSachLoaiSPMousePressed(evt);
            }
        });
        jScrollPane1.setViewportView(tbDanhSachLoaiSP);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 455, Short.MAX_VALUE)
            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 467, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 252, Short.MAX_VALUE)
            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 252, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnThemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThemActionPerformed
        try{                
            if(KiemTraTruocKhiThem()>0)
                return ;                           
            LoaiSanPham loaisp;                        
            loaisp = new LoaiSanPham(0, txtTenLoaiSP.getText().trim(),false,new HashSet());      
             LoaiSanPhamDAO.ThemLoaiSP(loaisp);             
             loaiSPs.add(loaisp);            
             Utility.CreateColumnsFromDB(LoaiSanPham.class, loaiSPs, tbDanhSachLoaiSP,new String[]{"Mã Loại Sản Phẩm","Tên Loại Sản Phẩm"},null);                          
             tbDanhSachLoaiSP.changeSelection(tbDanhSachLoaiSP.getModel().getRowCount()-1, 0, true, false);
             JOptionPane.showMessageDialog(this, "thêm thành công !");
        }catch(Exception ex){
            JOptionPane.showMessageDialog(this, "that bai"/*ex.getMessage()*/);
        }
    }//GEN-LAST:event_btnThemActionPerformed

    private void btnXoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnXoaActionPerformed
        Object []temp =Utility.GetSelectedRows(tbDanhSachLoaiSP, loaiSPs);
        if(temp.length!=0){
            if(JOptionPane.showConfirmDialog(this,"bạn có chắc muốn xóa loại sản phẩm này ?","Question",JOptionPane.YES_NO_OPTION )==0){
                try{
                    int vt =loaiSPs.indexOf(temp[0]);
                    LoaiSanPham loaisp =loaiSPs.get(vt);
                    loaisp.setIsDelete(true);
                    LoaiSanPhamDAO.XoaLoaiSP(temp);
                    Utility.RemoveSelectedRows(LoaiSanPham.class, tbDanhSachLoaiSP, loaiSPs, new String []{"Mã Loại Sản Phẩm","Tên Loại Sản Phẩm"}, null);
                   // Utility.ClearControlInPanel(null);
                    JOptionPane.showMessageDialog(this, "Xóa thành công");
                }catch(Exception ex){
                    JOptionPane.showMessageDialog(this, "xóa không thành công");
                }
            }
        }
        else{
            JOptionPane.showMessageDialog(this,"hãy chọn đối tượng muốn xóa bằng cách hãy click lên đối tượng ở dưới lưới");
        }
    }//GEN-LAST:event_btnXoaActionPerformed

    private void SetDataLoaiSanPhamToControls(LoaiSanPham loaisp){
        String maLoai =String.valueOf(loaisp.getId());
        txtMaLoaiSP.setText(maLoai);
        txtTenLoaiSP.setText(loaisp.getTenLoaiSanPham());        
    }

    private void tbDanhSachLoaiSPMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbDanhSachLoaiSPMouseClicked
        Object []temp =Utility.GetSelectedRows(tbDanhSachLoaiSP, loaiSPs);
        if(temp.length>0){
            LoaiSanPham loaisp =(LoaiSanPham)temp[0];
            SetDataLoaiSanPhamToControls(loaisp);
            Utility.CreateColumnsFromDB(LoaiSanPham.class, loaiSPs, tbDanhSachLoaiSP,new String[]{"Mã Loại Sản Phẩm","Tên Loại Sản Phẩm"},null);
            errSanPham.clear();
        }
    }//GEN-LAST:event_tbDanhSachLoaiSPMouseClicked

    private void btnSuaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSuaActionPerformed
         Object []temp =Utility.GetSelectedRows(tbDanhSachLoaiSP, loaiSPs);
        if(temp.length!=0){           
            try{
                int vt =loaiSPs.indexOf(temp[0]);
                LoaiSanPham loaisp =loaiSPs.get(vt); 
                loaisp.setTenLoaiSanPham(txtTenLoaiSP.getText().trim());
                LoaiSanPhamDAO.SuaLoaiSP(loaisp);
                Utility.CreateColumnsFromDB(LoaiSanPham.class, loaiSPs, tbDanhSachLoaiSP,new String[]{"Mã Loại Sản Phẩm","Tên Loại Sản Phẩm"},null);              
                JOptionPane.showMessageDialog(this, "Sửa thành công");
            }catch(Exception ex){
                JOptionPane.showMessageDialog(this, ex.getMessage());
            }            
        }
    }//GEN-LAST:event_btnSuaActionPerformed

    private void btnTimLoaiSPActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTimLoaiSPActionPerformed
         try {
            // TODO add your handling code here:
            Integer maLoaiSanPham = txtMaLoaiSP.getText().isEmpty() ? null : Integer.parseInt(txtMaLoaiSP.getText());
            loaiSPs = LoaiSanPhamDAO.TimKiemLoaiSanPham(maLoaiSanPham, txtTenLoaiSP.getText().trim(),false);
            Utility.CreateColumnsFromDB(LoaiSanPham.class, loaiSPs, tbDanhSachLoaiSP, new String[]{"Mã Loại Sản Phẩm","Tên Loại Sản Phẩm"}, null);
            if (loaiSPs.isEmpty()) {
                JOptionPane.showMessageDialog(this, "Không tìm thấy");
            }
        } catch (Exception ex) {
        //    logger.error(new Date().toString(), ex);
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
    }//GEN-LAST:event_btnTimLoaiSPActionPerformed

    private void tbDanhSachLoaiSPMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbDanhSachLoaiSPMousePressed
      
    }//GEN-LAST:event_tbDanhSachLoaiSPMousePressed

    private void btnClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnClearActionPerformed

        txtTenLoaiSP.setText("");
        txtMaLoaiSP.setText("");        
        Utility.RemoveSelectedRows(LoaiSanPham.class, tbDanhSachLoaiSP, loaiSPs, new String[]{"Mã Loại Sản Phẩm","Tên Loại Sản Phẩm"}, null);        
    }//GEN-LAST:event_btnClearActionPerformed

    /**
     * @param args the command line arguments
     */

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnClear;
    private javax.swing.JButton btnSua;
    private javax.swing.JButton btnThem;
    private javax.swing.JButton btnTimLoaiSP;
    private javax.swing.JButton btnXoa;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private org.jdesktop.swingx.JXTable tbDanhSachLoaiSP;
    private javax.swing.JTextField txtMaLoaiSP;
    private javax.swing.JTextField txtTenLoaiSP;
    // End of variables declaration//GEN-END:variables
}
